package com.uw.uwrpc.demo.provider;

import com.uw.uwrpc.core.annotation.UWService;
import com.uw.uwrpc.core.api.RpcContext;
import com.uw.uwrpc.demo.api.UserService;
import com.uw.uwrpc.demo.api.dto.User;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Slf4j
@UWService
@Component
public class UserServiceImpl implements UserService {
    @Value("${server.port}")
    private Integer port;

    @Override
    public User findById(int id) {
        if (id == 0) {
            return null;
        }

        if (id == -1) {
            throw new RuntimeException("invalid id.");
        }

        return new User(id, "name-" + port + "-" + System.currentTimeMillis());
    }

    @Override
    public User findById(long id) {
        return findById((int) id);
    }

    @Override
    public User findById(User user) {
        return findById(user.getId());
    }

    @Override
    public long count(long id) {
        log.debug("count(long id)");
        return id;
    }

    @Override
    public Long count(Long id) {
        log.debug("count(Long id)");
        return id;
    }

    @Override
    public int count(int id) {
        log.debug("count(int id)");
        return id;
    }

    @Override
    public List<User> list(int type) {
        List<User> users = new ArrayList<>();
        users.add(new User(type, "name-" + System.currentTimeMillis()));
        return users;
    }

    @Override
    public List<User> list(List<Integer> types) {
        List<User> users = new ArrayList<>();
        for (Integer type : types) {

            users.add(new User(type, "name-" + System.currentTimeMillis()));
        }
        return users;
    }

    @Override
    public List<User> listByUser(List<User> users) {
        return users;
    }

    @Override
    public String findNameById(int id) {
        return "name-" + System.currentTimeMillis();
    }

    @Override
    public int findTypeById(int id) {
        return id;
    }

    @Override
    public void delete(User user) {
        log.debug(" delete(User user) ");
    }

    @Override
    public void deleteAll() {
        log.debug("deleteAll()");
    }

    @Override
    public int[] findTypeByIds(int[] id) {
        return id;
    }

    @Override
    public long[] findTypeByIds(long[] id) {
        return id;
    }

    @Override
    public User[] findTypeByIds(User[] id) {
        return id;
    }


    @Override
    @SneakyThrows
    public User timeout(int timeout) {
        if (port == 7071) {
            TimeUnit.MILLISECONDS.sleep(timeout);
        }
        return User.builder().name("timeout-" + port + "-" + timeout).build();
    }

    @Override
    public String echoParameter(String param) {
        RpcContext.getContextParam().forEach((k, v) -> log.info("key:{},value:{}", k, v));
        return RpcContext.getContextParam(param);
    }
}
